package nsu.edu.zsq.controller.sys;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import nsu.edu.zsq.bean.Message;
import nsu.edu.zsq.common.JsonData;
import nsu.edu.zsq.param.PageQuery;
import nsu.edu.zsq.service.sys.MessageService;

/** 
* Description: 学校通知的controller类<br>
*/
@RequestMapping("/sys/message")
@Controller
public class MessageController {

    @Autowired
    private MessageService messageService;
    
    /**
     * 获取学校通知列表
     * @param pageQuery 分页数据
     * @return JsonData
     */
    @RequestMapping("/listMessage")
    @ResponseBody
    public JsonData listMessage(PageQuery pageQuery) {
        PageInfo<Message> listMessageWithPage = messageService.listMessageWithPage(pageQuery);
        return JsonData.success(listMessageWithPage);
    }
    
    /**
     * 保存一条学校通知
     * @param message
     * @return JsonData
     */
    @RequestMapping("/saveMessage")
    @ResponseBody
    public JsonData saveMessage(Message message) {
        messageService.saveMessage(message);
        return JsonData.success();
    }
    
    /**
     * 跳转到编辑学校通知页面
     * @param messageId 消息id
     * @param model 
     * @return String "manager/message/message_edit.html"
     */
    @RequestMapping("/toEditMessage")
    public String toEditMessage(@RequestParam("messageId")int messageId, Model model) {
        Message message = messageService.getMessage(messageId);
        model.addAttribute("message", message);
        return "manager/message/message_edit.html";
    }
    
    /**
     * 更新一条学校通知
     * @param message
     * @return JsonData
     */
    @RequestMapping("/updateMessage")
    @ResponseBody
    public JsonData updateMessage(Message message) {
        messageService.updateMessage(message);
        return JsonData.success();
    }
    
    /**
     * 单个/批量删除学校通知
     * @param messageIds 通知id数组
     * @return JsonData
     */
    @RequestMapping("/delMessage")
    @ResponseBody
    public JsonData delMessage(@RequestParam("messageIds[]")int[] messageIds) {
        messageService.deleteMessage(messageIds);
        return JsonData.success();
    }
}
